package com.linkcard.single.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.single.manager.SingleManager;
import com.linkcard.single.model.SinglePage;
import com.linkcard.user.model.WebsiteColumns;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SingleAction extends ActionSupport {

	private SingleManager singleManager=null;
	private int columnId;
	private SinglePage singlePage=null;
	private List list=null;
	private String template=null;
	private String path=null;
	@Override
	public String execute() throws Exception {
		List columnList=(List)ActionContext.getContext().getApplication().get("columns");
		for(int i=0;i<columnList.size();i++)
		{
			WebsiteColumns column=(WebsiteColumns)columnList.get(i);
			if(column.getId()==columnId)
			{
				template=column.getTemplate();
				path="<a href='"+column.getUrl()+".action?columnId="+column.getId()+"'>"+column.getName()+"</a>";
				break;
			}
		}
		list=this.singleManager.singlePageList(columnId);
		for(int i=0;i<list.size();i++)
		{
			SinglePage single=(SinglePage)list.get(i);
			if(singlePage==null)
			{
				singlePage=single;
				break;
			}
			else if(single.getId().equals(singlePage.getId()))
			{
				singlePage=single;
				
				break;
			}
		}
		if(singlePage!=null)
		{
		path+=">><a href='singlePage?columnId="+columnId+"&singlePage.id="+singlePage.getId()+"'>"+singlePage.getTitle()+"</a>";
		path="<span>位置："+path+"</span><h1>"+singlePage.getTitle()+"</h1>";
		}
		return "success";
	}
	//添加单页面
	public String addSinglePage() throws Exception{
		  boolean  isSuccess=this.singleManager.addSinglePage(singlePage);
		  if(!isSuccess)
		  {
			  this.addActionMessage("添加失败");
			  return "SinglePage";
		  }
		return "List";
	}
	//单页面列表
	public String singlePageList() throws Exception{
		List  list=this.singleManager.singlePageList(singlePage.getColumnId());
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			SinglePage singlePage=(SinglePage)list.get(i);
			JSONObject tempjson=new JSONObject();
			tempjson.put("id",singlePage.getId());
			tempjson.put("title",singlePage.getTitle());
			tempjson.put("sort", singlePage.getSort());
			tempjson.put("opt", "<a href='singlePage.action?columnId="+singlePage.getColumnId()+"&singlePage.id="+singlePage.getId()+"' target='_blank'>查看</a>&nbsp;&nbsp;<a href='singlePage!modify.action?singlePage.columnId="+singlePage.getColumnId()+"&singlePage.id="+singlePage.getId()+"'>修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteSinglePage("+singlePage.getId()+")'>删除</a>");
			jsonArray.add(tempjson);
		}
		json.put("rows",jsonArray);
		backText(json.toString());
		return "Data";
	}
	//修改单页面
	public String modify() throws Exception{
		this.clearMessages();
		singlePage=this.singleManager.findSinglePage(singlePage.getId());
		return "SinglePage";
	}
	public String modifySinglePage() throws Exception{
		boolean isSuccess=this.singleManager.modifySinglePage(singlePage);
		if(!isSuccess)
		{
			this.addActionMessage("修改失败");
			return "SinglePage";
		}
		return "List";
	}
	//删除单页面
	public String deleteSinglePage() throws Exception{
		boolean isSuccess=this.singleManager.deleteSinglePage(singlePage.getId());
		JSONObject json=new JSONObject();
		json.put("info",isSuccess);
		backText(json.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public SingleManager getSingleManager() {
		return singleManager;
	}
	public void setSingleManager(SingleManager singleManager) {
		this.singleManager = singleManager;
	}
	public SinglePage getSinglePage() {
		return singlePage;
	}
	public void setSinglePage(SinglePage singlePage) {
		this.singlePage = singlePage;
	}
	public List getList() {
		return list;
	}
	public int getColumnId() {
		return columnId;
	}
	public void setColumnId(int columnId) {
		this.columnId = columnId;
	}
	public String getTemplate() {
		return template;
	}
	public void setTemplate(String template) {
		this.template = template;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
}
